<?php

namespace app\index\controller;

use app\common\model\Ad;
use app\common\model\Cost;
use app\common\model\PayLog;
use app\common\model\Sort;
use think\Controller;
use think\facade\Session;
use think\Request;
use app\common\model\User as us;

class User extends Controller
{
    protected function initialize()
    {
        //如果已登陆则头部修改
        if (Session::has('main_user')) {
            $this->assign('login', Session::get('main_user'));
            $sort=new Sort();
            $sort_list_menu = $sort->get_Sort_menu();
            $this->assign('sort_list_menu', $sort_list_menu);
        } else {
            return $this->redirect('index/index/login');
            // $this->assign('login', '0');
        }
    }
    //用户主页
    public function main()
    {
        $site_info =  seo('会员中心', 5);
        //防止用户开通vip等，所以每次进入刷新user信息
        $s_u = Session::get('main_user');
        $u = new us;
        $user = $u->getinfo($s_u['Id']);
        $cost = new Cost();
        $i = new PayLog();
        $num = $i->getUserNum();

        $this->assign([
            'site_seo' => $site_info,
            'user' => $user,
            'ratio'=>Get_Ratio(1),
            'cost' => $cost->getinfo(),
            'invite_num'=>$num
        ]);
        return $this->fetch();
    }
    //修改密码
    public function password()
    {
        $u = new us;
        $s_u = Session::get('main_user');
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $re = $u->up_pass($data, $s_u['Id']);
            if ($re === true) {
                Session::clear();
                return $this->success('修改成功！');
            }
            return $this->success($re);
        } else {
            $site_info =  seo('修改密码', 5);

            $user = $u->getinfo($s_u['Id']);
            $cost = new Cost();
            $this->assign([
                'site_seo' => $site_info,
                'user' => $user,
                'cost' => $cost->getinfo()
            ]);
            return $this->fetch();
        }
    }
    //开通会员‘】
    public function open_vip()
    {
        $u = new us;
        $s_u = $u->getinfo();
        $cost = new Cost();
        $cost_s =$cost->getinfo(0);
        if ($this->request->isPost()) {
            $time = $this->request->param('time');
            //计算用户余额是否足够
            $price = $time * Get_Ratio($cost_s['vip_price']);
            if($s_u['balance'] < $price){
                return json(['code'=>-1,'msg'=>'账户题币余额不足']);
            }
            $data=[
                'price'=>$price,
                'time'=>$time
            ];
            if($u->op_vip($data)){
                return  json(['code'=>1,'msg'=>'开通成功']);
            }
            return json(['code'=>-1,'msg'=>'开通失败 请联系客服']);
        } else {
            $site_info =  seo('开通会员', 5);

            $user = $u->getinfo($s_u['Id']);

            $this->assign([
                'site_seo' => $site_info,
                'user' => $user,
                'cost' => $cost_s,
                'ratio'=>Get_Ratio(1),
            ]);
            return $this->fetch();
        }
    }
    //充值
    public function pay()
    {
        $u = new us;
        $s_u = Session::get('main_user');
        $cost = new Cost();
        $pay_info = $cost->getinfo(0);
        if ($this->request->isPost()) {
            //充值入库记录
            $data = $this->request->param();
            $l = new PayLog();
            $pay_order = GetOrder();
            $l->in_log($pay_order,$s_u['Id'],$data['money'],$data['pay_type']);
            //跳转付款页面
            //处理码支付类型
            if($data['pay_type'] == '1'){
                $data['pay_type'] = '3';
            }
            if($data['pay_type'] == '2'){
                $data['pay_type'] = '1';
            }
            $url = 'https://api.xiuxiu888.com/creat_order/?id=' . $pay_info['mzf_id'] . '&token=' . $pay_info['mzf_sign'] . '&price=' . $data['money'] . '&pay_id=' . $pay_order . '&type='.$data['pay_type'].'&page=1&return_url='.$this->request->domain().'/index/user/main.html';
            return $this->redirect($url);

        } else {
            $site_info = seo('充值积分', 5);

            $user = $u->getinfo($s_u['Id']);

            $this->assign([
                'site_seo' => $site_info,
                'user' => $user,
                'cost' => $pay_info,
                'ratio'=>Get_Ratio(1),
            ]);
            return $this->fetch();
        }
    }
    public function invite_list()
    {
        $i = new PayLog();
        $list = $i->getList();
        $site_info =  seo('推广列表', 5);
        $this->assign([
            'list'=> $list,
            'site_seo' => $site_info,
        ]);

        return $this->fetch();
    }
}
